<?php
/**
 * Created by YuanXiShop.
 * User: YuanXi
 * Date: 2021/6/3 0003
 * Time: 1:26
 */

namespace admin;


use models\BaseDao;
use Slince\Upload\UploadHandlerBuilder;

class Setting extends Admin
{
    function __construct()
    {
        parent::__construct();
        $this->assign('menumark','setting');
    }
    function index()
    {
        //获取数据库操作对象
        $db = new BaseDao();
        //获取全部友情链接，并按ord排序
        $data = $db->select('setting','*');
        //将数据分配给模板
        foreach ($data as $v){
            $this->assign($v['skey'],$v['svalue']);
        }
        //标题
        $this->assign('title','基本设置');
        //输出模板
        $this->display('setting/mod');
    }

    function doupdate()
    {

        if($_FILES['web_logo']['error']==0) {
            $path = TEMPDIR."/uploads";

            $builder = new UploadHandlerBuilder(); //create a builder.
            $handler = $builder

                //add constraints

                ->allowExtensions(['jpg', 'png', 'gif'])
                ->allowMimeTypes(['image/*'])

                ->saveTo($path) //save to local
                ->getHandler();


            $files = $handler->handle();
            $filename = $files['web_logo']->getUploadedFile()->getClientOriginalName();

            $newfilename =  date('Y-md') . '-' . uniqid() . '.' . $files['web_logo']->getUploadedFile()->getClientOriginalExtension();

            rename($path.'/'.$filename, $path.'/'.$newfilename);

            $_POST['web_logo'] = $newfilename;
        }


        $db = new BaseDao();
        $num = 0;

        foreach($_POST as $k => $v) {
            $num += $db->update('setting', ['svalue'=>$v], ['skey'=>$k]);
        }

        if($num) {
            $this->success('/admin/setting', '基本信息设置成功！');
        }else{
            $this->error('admin/setting', '基本信息设置失败！');
        }
    }
}